;(function ($, window, document, undefined) {
    //定义构造函数
    var sku = function (ele, opt) {
        this.$element = ele;
        this.defaults = {
            'skulist':[]
        };
        this.options = $.extend({}, this.defaults, opt);
    }
    sku.prototype = {
        init: function () { //初始化方法
            var _ = this;
            _.selectAttr();
            $('.choose-attrs').on('click','.item',function () {
                if ($(this).hasClass('disabled')) {
                    return;//被锁定了
                }
                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected');
                } else {
                    $(this).siblings().removeClass('selected');
                    $(this).addClass('selected');
                }
                _.selectAttr();
                $('.num').trigger('change');
            });
        },
        selectAttr:function () {
            var _=this;
            var select_ids = _.getSelAttrId();
            //已经选择了的规格
            var $_sel_goods_attr = $('.item.selected').parents('.goods-attr');
            // step 1
            var all_ids = _.filterAttrs(select_ids);
            //比较选择的规格个数是否和键值个数是否一样
            if ($('.item.selected').length == all_ids.length) {
                //根据键值查询数据对应信息,并赋值
                $.each(skulist, function (idx, obj) {
                    sel_sku_key = all_ids.join(';');
                    if (obj['sku_key'] == sel_sku_key) {
                        var base={
                            'sku_id':obj.sku_id,
                            'goods_price':obj.goods_price,
                            'min':obj.min,
                            'step':obj.step,
                            'stock':obj.goods_stock
                        }
                        $('.choose-attrs').data('base',obj);
                    }
                });
            } else {
                $('.choose-attrs').data('base',{sku_id:0});
            }
            //获取未选择的
            var $other_notsel_attr = $('.goods-attr').not($_sel_goods_attr);
            //设置为选择属性中的不可选节点
            $other_notsel_attr.each(function () {
                _.setBlock($(this), all_ids);
            });
            //step 2
            //设置已选节点的同级节点是否可选
            $_sel_goods_attr.each(function () {
                _.update($(this));
            });
        },
        filterProduct: function (ids) { ////获取所有包含指定节点的路线
            var _ = this;
            var result = [];
            $(_.options.skulist).each(function (k, v) {
                _attr = ';' + v['sku_key'] + ';';
                _all_ids_in = true;
                for (k in ids) {
                    if (_attr.indexOf(';' + ids[k] + ';') == -1) {
                        _all_ids_in = false;
                        break;
                    }
                }
                if (_all_ids_in) {
                    result.push(v);
                }

            });
            return result;
        },
        filterAttrs: function (ids) { //获取 经过已选节点 所有线路上的全部节点 根据已经选择得属性值，得到余下还能选择的属性值
            var _=this;
            var products = _.filterProduct(ids);
            //console.log(products);
            var result = [];
            $(products).each(function (k, v) {
                result = result.concat(v['sku_key'].split(';'));
            });
            return result;
        },
        update: function ($goods_attr) {
            var _=this;
            // 若该属性值 $li 是未选中状态的话，设置同级的其他属性是否可选
            var select_ids = _.getSelAttrId();
            var $item = $goods_attr.find('.item.selected');
            var select_ids2 = _.delArrayVal(select_ids, $item.attr('val'));
            var all_ids = _.filterAttrs(select_ids2);
            _.setBlock($goods_attr, all_ids);
        },
        setBlock: function ($goods_attr, all_ids) {
            var _=this;
            //根据 $goods_attr下的所有节点是否在可选节点中（all_ids） 来设置可选状态
            $goods_attr.find('.item').each(function (k2, item) {
                if ($.inArray($(item).attr('val'), all_ids) == -1) {
                    $(item).addClass('disabled');
                } else {
                    $(item).removeClass('disabled');
                }
            });
        },
        delArrayVal: function (arr, val) {
            var _=this;
            //去除 数组 arr中的 val ，返回一个新数组
            var a = [];
            for (k in arr) {
                if (arr[k] != val) {
                    a.push(arr[k]);
                }
            }
            return a;
        },
        //已选择的节点数组
        getSelAttrId: function () {
            var list = [];
            $('.goods-attr .item.selected').each(function () {
                list.push($(this).attr('val'));
            });
            return list;
        }
    }
    $.fn.skuPlugin = function (options) {
        var skuPlugin = new sku(this, options);
        skuPlugin.init();
        return skuPlugin;
    }
})(jQuery, window, document);